﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _4T.BLL;
using _4T.Data;

namespace _4T
{
    public partial class ProfileView_CMS : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["p"] != null)
            {
                string profileCode = Request.QueryString["p"].ToString();
                LoadProfileInfo(profileCode);
                LoadProfileStatus(profileCode);
                hplEdit.NavigateUrl = "ProfileInfoAdd.aspx?p=" + profileCode;
            }
        }

        private void LoadProfileStatus(string profileCode)
        {
            ProfileBLL profileBLL = new ProfileBLL();
            CMS_Profile profile = profileBLL.GetProfileByProfileCode(profileCode);
            if (profile != null)
            {
                switch (profile.Stage)
                {
                    case 2: { lbStage.Text = "Sơ tuyển"; btnFilter.Visible = true; } break;
                    case 3: { lbStage.Text = "Xác minh";  } break;
                    case 4: lbStage.Text = "Phê duyệt"; break;
                    case 5: lbStage.Text = "Thanh toán"; break;
                }
                switch (profile.Status)
                {
                    case 1: lbStatus.Text = "Đang chờ"; break;
                    case 2: lbStatus.Text = "Đang thực hiện"; break;
                    case 3: lbStatus.Text = "Thành công"; break;
                    case 4: lbStatus.Text = "Thất bại"; break;
                    default: lbStatus.Text = "Đang chờ";
                        break;
                }
                lbLastEditTime.Text = profile.LastEditTime;
            }
        }

        private void LoadProfileInfo(string profileCode)
        {
            ProfileInfoBLL infoBLL = new ProfileInfoBLL();
            CMS_ProfileInfo info = infoBLL.GetLastProfileInfoByProfileCode(profileCode);
            if (info != null)
            {
                hdfProfileInfoCode.Value = info.ProfileInfoCode;

                // Load patient info
                PatientInfoBLL patientBLL = new PatientInfoBLL();
                PatientInfo patient = patientBLL.GetPatientInfo(info.ProfileInfoCode);
                if (patient != null)
                {
                    txtPatientName.Text = patient.PatientName;
                    txtDOB.Text = patient.Birthday.ToShortDateString();
                    ltrDOB.Text = patient.Gender;
                    txtIDNumber.Text = patient.IDNumber;
                    txtPermAddr.Text = patient.PermanentAddress;
                    txtTempAddr.Text = patient.TemporaryAddress;
                    txtMobileNumber.Text = patient.MobileNumber;
                    txtPhoneNumber.Text = patient.PhoneNumber;
                    txtRepresentativeName.Text = patient.EmergencyRepresentative;
                    txtRepresentativePhone.Text = patient.RepresentativePhoneNumber;
                    txtLocalContactName.Text = patient.LocalContactPoint;
                    txtLocalContactPhone.Text = patient.LocalContactPointPhoneNumber;
                    txtMedicalStaffName.Text = patient.LocalMedicalStaff;
                    txtMedicalStaffPhone.Text = patient.LocalMedicalStaffPhoneNumber;
                    txtMedicalFacility.Text = patient.MedicalFacility;
                    txtNote.Text = patient.Note;
                }

                // Load medical info
                MedicalInfoBLL medicalBLL = new MedicalInfoBLL();
                MedicalInfo medical = medicalBLL.GetMedicalInfo(info.ProfileInfoCode);
                if (medical != null)
                {
                    txtDiagnosis.Text = medical.Diagnosis;
                    txtMedicalTime.Text = medical.MedicalTime.ToShortDateString();
                    txtTreatmentMethod.Text = medical.TreatmentMethod;
                    txtSituation.Text = medical.Situation;
                    txtSupport.Text = medical.Support;
                }


                // Load info supplier info
                InfoSupplierInfoBLL infoSupplierBLL = new InfoSupplierInfoBLL();
                InfoSupplierInfo supplier = infoSupplierBLL.GetInfoSupplierInfo(info.ProfileInfoCode);
                if (supplier != null)
                {
                    txtInfoSupplierName.Text = supplier.SupplierName;
                    txtInfoSupplierPhone.Text = supplier.PhoneNumber;
                    txtPersonalCode.Text = supplier.PersonalCode;
                    txtEmail.Text = supplier.Email;
                }
            }
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            string url = "Filter.aspx?pi=" + hdfProfileInfoCode.Value;
            Response.Redirect(url);
        }
    }
}